package com.jams.goodox.net;


import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.jams.goodox.BuildConfig;
import com.jams.goodox.SePref;
import com.jams.goodox.entity.PageInfo;
import com.jams.goodox.utils.DeviceUtils;
import com.jams.goodox.utils.StringUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * Created by 顾佳佳 on 2017/4/17.
 */
public class ProtocolManagers {

    private Retrofit retrofit;
    private static ProtocolManagers mInstance;
    private static ProtocolManagers mContextInstance;
    private static final int CONNECT_TIMEOUT_MILLIS = 30 * 1000; // 30s
    private static final int READ_TIMEOUT_MILLIS = 30 * 1000; // 30s
    public static final String SUCCESS = "100";//网络请求成功
    private ProtocolManagers() {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
                .writeTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
                .readTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }

    public static ProtocolManagers getInstance(Context context) {
        return null == mContextInstance ? mContextInstance = new ProtocolManagers() : mContextInstance;
    }

    public static ProtocolManagers getInstance() {
        return null == mInstance ? mInstance = new ProtocolManagers() : mInstance;
    }

    public static RequestBody getRequestMap(String requestData){
        Map<String, String> map=new ArrayMap<>();
        map.put("accessToken",SePref.getToken());
        map.put("timeStamp",String.valueOf(System.currentTimeMillis()));
        map.put("uuid",DeviceUtils.getUUID());
        map.put("version",BuildConfig.VERSION_NAME);

        map.put("requestData",requestData);
        RequestBody requestBody= RequestBody.create(MediaType.parse("application/json"),new Gson().toJson(map));
        return requestBody;
    }

    public static <T> RequestBody getRequestMap(T object){
        if(object==null)return null;
        else return getRequestMap(new Gson().toJson(object));
    }

    public static RequestBody getDefaultMap(){
        return getRequestMap(new Gson().toJson(new MineRequest(String.valueOf(SePref.getId()))));
    }

    public static class MineRequest {

        public MineRequest(String memberId) {
            this.memberId = memberId;
        }

        public String memberId;

    }

    /**
     * 根据指定的Service.Class ，得到一个被代理后Service
     *
     * @param service 被创建的参数
     * @param <T>     传入泛型解析对象
     * @return 返回创建的service
     */
    public <T> T getService(Class<T> service) {
        return retrofit.create(service);
    }
}
